package cn.wolfcode.education.service.impl;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Permission;
import cn.wolfcode.education.mapper.PermissionMapper;
import cn.wolfcode.education.query.QueryObject;
import cn.wolfcode.education.service.IPermissionService;
import cn.wolfcode.education.util.PageResult;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;


    public PageResult list(QueryObject qo) {
        Integer totals = permissionMapper.queryForCount(qo);
        if(totals == 0){
            return new PageResult();
        }
        List<Permission> rows = permissionMapper.queryForList(qo);
        return new PageResult(totals,rows);
    }

    //加载 权限
    public void reload() {

        List<String> permissionsNames = permissionMapper.selectAllPermissionName();
        //1.获取所有的Controller对象
        Map<String, Object> controllers = ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> cons = controllers.values();

        //2.遍历获取每一个controller中的所有方法
        for (Object obj : cons) {
            Method[] mets = obj.getClass().getDeclaredMethods();

            //3.遍历获取每一个方法
            for (Method met : mets) {
                //4.获取每一个方法上的 权限相关标签
                PermissionName permissionName = met.getAnnotation(PermissionName.class);
                if (permissionName != null) {

                    //权限的名字
                    String name = permissionName.value();

                    //5.判断该权限是否已经在数据库
                    if (!permissionsNames.contains(name)) {

                        //权限表达式标签
                        RequiresPermissions requiresPermissions = met.getAnnotation(RequiresPermissions.class);
                        if (requiresPermissions != null){

                            String[] resources = requiresPermissions.value();
                            System.out.println(resources);

                            //把表达式进行拼接 ,如  :employee:view,employee:show
                            String resource = StringUtils.join(resources, ",");

                            //6.根据标签的值,封装成permission对象,保存到数据库
                            Permission permission = new Permission();
                            permission.setName(name);
                            permission.setResource(resource);
                            permissionMapper.insert(permission);
                        }
                    }
                }

            }
        }

    }

    public PageResult listAll(QueryObject qo) {
        List<Permission> permissions = permissionMapper.selectAll();
        return new PageResult(0,permissions);
    }

    public List<Permission> queryForRoleId(Long roleId) {
       return permissionMapper.selectPermissionByRoleId(roleId);
    }
}
